package actions.profesionales;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import model.Profesional;

import org.apache.struts2.interceptor.SessionAware;

import DAO.ProfesionalDAO;

import com.opensymphony.xwork2.ActionSupport;

public class DatosTabla extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 5078264277068533593L;

	private List<Profesional> gridModel;
	private Integer rows = 0;
	private Integer page = 0;
	private Integer total = 0;
	private Integer record = 0;
	private String sord;
	private String sidx;

	// b�squeda
	private String searchField;
	private String searchString;
	private String searchOper;

	@SuppressWarnings("unused")
	private Map<String, Object> session;
	private List<Profesional> profesionales = new ArrayList<Profesional>();

	public String execute() {
		ProfesionalDAO pDAO = new ProfesionalDAO();
		int to = (rows * page);
		int from = to - rows;

		this.profesionales = (List<Profesional>) pDAO.listar(from, to, sidx, sord);


		// Contar filas
		record = pDAO.contar();

		// Seteo el gridModel con la lista obtenida
		gridModel = profesionales;

		// Calcular el n�mero de p�ginas para la consulta
		total = (int) Math.ceil((double) record / (double) rows);

		return SUCCESS;
	}

	public List<Profesional> getprofesionales() {
		return profesionales;
	}

	public String getJSON() {
		return execute();
	}

	/**
	 * @return how many rows we want to have into the grid
	 */
	public Integer getRows() {
		return rows;
	}

	/**
	 * @param rows
	 *            how many rows we want to have into the grid
	 */
	public void setRows(Integer rows) {
		this.rows = rows;
	}

	/**
	 * @return current page of the query
	 */
	public Integer getPage() {
		return page;
	}

	/**
	 * @param page
	 *            current page of the query
	 */
	public void setPage(Integer page) {
		this.page = page;
	}

	/**
	 * @return total pages for the query
	 */
	public Integer getTotal() {
		return total;
	}

	/**
	 * @param total
	 *            total pages for the query
	 */
	public void setTotal(Integer total) {
		this.total = total;
	}

	/**
	 * @return total number of records for the query. e.g. select count(*) from
	 *         table
	 */
	public Integer getRecord() {
		return record;
	}

	/**
	 * @param record
	 *            total number of records for the query. e.g. select count(*)
	 *            from table
	 */
	public void setRecord(Integer record) {

		this.record = record;

		if (this.record > 0 && this.rows > 0) {
			this.total = (int) Math.ceil((double) this.record
					/ (double) this.rows);
		} else {
			this.total = 0;
		}
	}

	/**
	 * @return an collection that contains the actual data
	 */
	public List<Profesional> getGridModel() {
		return gridModel;
	}

	/**
	 * @param gridModel
	 *            an collection that contains the actual data
	 */
	public void setGridModel(List<Profesional> gridModel) {
		this.gridModel = gridModel;
	}

	/**
	 * @return sorting order
	 */
	public String getSord() {
		return sord;
	}

	/**
	 * @param sord
	 *            sorting order
	 */
	public void setSord(String sord) {
		this.sord = sord;
	}

	/**
	 * @return get index row - i.e. user click to sort.
	 */
	public String getSidx() {
		return sidx;
	}

	/**
	 * @param sidx
	 *            get index row - i.e. user click to sort.
	 */
	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public String getSearchField() {
		return searchField;
	}

	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}

	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	public String getSearchOper() {
		return searchOper;
	}

	public void setSearchOper(String searchOper) {
		this.searchOper = searchOper;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

}
